﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class PromotionDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Get all Promotion in Order By PromotionName
        /// </summary>
        /// <author>vuongvt</author>
        /// <returns>List of Promotion</returns>
        public List<Promotion> GetAllPromotions()
        {
            try
            {
                List<Promotion> listPromotion = new List<Promotion>();
                var promotions = from promotion in _entities.Promotions
                                 where promotion.IsDeleted == false
                                 orderby promotion.PromotionName
                                 select promotion;
                if (promotions != null)
                {
                    listPromotion = promotions.ToList();
                }
                return listPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list promotion for Create Schedule
        /// </summary>
        /// <returns>List of Promotion have FromDate >= Today</returns>
        /// <author>VuongVT</author>
        public List<Promotion> GetListPromotionForCreateSchedule()
        {
            try
            {
                List<Promotion> listPromotion = new List<Promotion>();
                var resultQuery = from promotion in _entities.Promotions
                                  where promotion.IsDeleted == false
                                        && promotion.ToDate >= DateTime.Today
                                  orderby promotion.PromotionName
                                  select promotion;
                foreach (var item in resultQuery)
                {
                    listPromotion.Add(item);
                }
                return listPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list of Promotions Order By PromotionId
        /// </summary>
        /// <returns>List of promotions</returns>
        /// <author>VuongVT</author>
        /// <date>3/7/2014</date>
        public List<Promotion> LoadPromotionsOrderById()
        {
            try
            {
                List<Promotion> listPromotion = new List<Promotion>();
                var promotions = from promotion in _entities.Promotions
                                 where promotion.IsDeleted == false
                                 orderby promotion.PromotionId descending
                                 select promotion;
                if (promotions != null)
                {
                    listPromotion = promotions.ToList();
                }
                return listPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Select promotion by schedule
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public Promotion GetPromotionByScheduleId(Schedule schedule)
        {
            try
            {
                Promotion promotionEntities = new Promotion();
                var selectPromotion = (from promotion in _entities.Promotions
                                       where promotion.PromotionId == schedule.PromotionId
                                             && promotion.IsDeleted == false
                                       select promotion).FirstOrDefault();
                promotionEntities = selectPromotion;
                return promotionEntities;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get a promotion by Promotion Id
        /// </summary>
        /// <returns>Promotion</returns>
        /// <author>VuongVT</author>
        /// <date>3/7/2014</date>
        public Promotion LoadPromotionById(int id)
        {
            try
            {
                Promotion promotion = new Promotion();
                promotion = (from promotionEnt in _entities.Promotions
                             where promotionEnt.PromotionId == id
                                   && promotionEnt.IsDeleted == false
                             select promotionEnt).FirstOrDefault();
                return promotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add a promotion
        /// </summary>
        /// <returns>int</returns>
        /// <author>VuongVT</author>
        /// <date>3/7/2014</date>
        public int Add(Promotion promotion)
        {
            try
            {
                _entities.Promotions.Add(promotion);
                return _entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit a Film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Edit(Promotion promotion)
        {
            try
            {
                _entities.Entry(promotion).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// delete promotion
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/7/2014</date>
        public int Delete(Promotion promotion)
        {
            try
            {
                promotion.IsDeleted = true;
                _entities.Entry(promotion).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if promotion name is existed or not when create
        /// </summary>
        /// <param name="promotion"></param>
        /// <returns>true || false</returns>
        /// <author>hungph</author>
        /// <date>3/7/2014</date>
        public bool CheckIsExistedCreate(Promotion promotion)
        {
            try
            {
                var isExist = _entities.Promotions.Any(c => c.PromotionName == promotion.PromotionName);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check is exist the promotion name when update
        /// </summary>
        /// <param name="promotion"></param>
        /// <returns></returns>
        /// <author>VuongVT</author>
        public bool CheckIsExistedEdit(Promotion promotion)
        {
            try
            {
                var isExist = _entities.Promotions.Any(c => c.PromotionId != promotion.PromotionId &&  c.PromotionName == promotion.PromotionName);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Promotion GetPromotionById(int id)
        {
            return _entities.Promotions.Find(id);
        }

        /// <summary>
        /// Check the ShowTimeId is used in Schedule and have Booked ticket or not
        /// This function is used for user Click to Edit in List Promotion
        /// </summary>
        /// <param name="showTimeId"></param>
        /// <returns>True: Is Exist ---- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsPromotionUsedForScheduleAndBooked(int promotionId)
        {
            try
            {
                bool isUsedSchedule = false;

                var scheduleId = (from schedule in _entities.Schedules
                                  where schedule.PromotionId == promotionId
                                        && schedule.IsDeleted == false
                                        && schedule.IsCreateTicket == true
                                  select schedule.ScheduleId).FirstOrDefault();
                if (scheduleId != 0)
                {
                    var isBookedTicket = _entities.Tickets.Any(ticket => ticket.ScheduleId == scheduleId
                                                                     && ticket.IsDeleted == false
                                                                     && ticket.Status == true);
                    if (isBookedTicket)
                    {
                        isUsedSchedule = true;
                    }
                }
                return isUsedSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check the ShowTimeId is used in Schedule or not
        /// This function is used for user Click to Delete in List Promotion
        /// </summary>
        /// <param name="showTimeId"></param>
        /// <returns>True: Is Exist ---- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsPromotionUsedForSchedule(int promotionId)
        {
            try
            {
                var isUsedForSchedule = _entities.Schedules.Any(schedule => schedule.PromotionId == promotionId
                                                                            && schedule.IsDeleted == false);
                return isUsedForSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}